<template>
  <div class="home">
    <el-table
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="最近两年大学英语四级听力试卷列表">
        <el-table-column prop="examinationName" label="试卷名称">
        </el-table-column>
        <el-table-column prop="examTime" label="考试时长"> </el-table-column>
        <el-table-column label="是否考过">
          <template slot-scope="scope">
            {{ scope.row.isDown == 1 ? "是" : "否" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="enterExam(scope.row)">
              {{ scope.row.isDown == 1 ? "查看试卷" : "开始考试" }}</el-button
            >
            <el-button type="text" @click="setCollection(scope.row)"
              >{{ scope.row.isCollection ? "取消收藏" : "收藏试题" }}
            </el-button>
            <el-button type="text" @click="enterEstimate(scope.row)"
              >评价试题</el-button
            >
          </template>
        </el-table-column>
      </el-table-column>
    </el-table>
    <el-pagination
      align="center"
      background
      layout="total,prev, pager, next"
      :page-size="5"
      :total="total"
      @current-change="currentChange"
    >
    </el-pagination>
  </div>
</template>


  <script>
import { getExamList, setCollection } from "@/api/user";
export default {
  data() {
    return {
      tableData: [],
      currPage: 1,
      total: 0,
    };
  },
  created() {
    this.getExamList();
  },
  methods: {
    async getExamList() {
      let nowYear = new Date().getFullYear();
      let hot = nowYear + "," + (nowYear - 1) + "," + (nowYear - 2);
      let params = {
        currPage: this.currPage,
        pageSize: 5,
        hot,
      };
      let {
        data: {
          data: { list, total },
        },
      } = await getExamList(params);
      this.tableData = list;

      this.total = total;
    },
    currentChange(v) {
      this.currPage = v;
      this.getExamList();
    },
    enterExam(row) {
      this.$router.push({
        path: row.isDown ? "/exam-check" : "/exam",
        query: row,
      });
    },
    enterEstimate(row) {
      let { isDown } = row;
      if (!isDown) {
        this.$modal.msgWarning("未考完的试卷不能进行评价！");
        return;
      }
      this.$router.push({
        path: "/comment",
        query: row,
      });
    },
    async setCollection(row) {
      let { id, isCollection, isDown } = row;
      if (!isDown) {
        this.$modal.msgWarning("未考完的试卷不能进行收藏！");
        return;
      }
      this.$set(row, "isCollection", !isCollection);
      let params = { id };
      let {
        data: { msg },
      } = await setCollection(params);
      this.$modal.msgSuccess(msg);
    },
  },
};
</script>

<style lang='scss' scoped>
.home {
  .el-table {
    margin: 20px auto;
    width: 1000px !important;
  }
}
.home ::v-deep .is-group .el-table__cell {
  background: #fff;
}
.home ::v-deep .is-group tr:first-child .el-table__cell {
  background: rgb(222, 184, 137);
  color: #000;
  height: 60px;
}
</style>